package com.cskaoyan.market.controller.wx;/*
 *文件名: WxGoodsController
 *创建者:wxin
 *创建时间:2024/5/20 14:14
 */

import com.cskaoyan.market.annotation.FootprintAdd;
import com.cskaoyan.market.bo.GoodBo;
import com.cskaoyan.market.config.SystemConfig;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.service.wx.*;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.CommentVo;
import com.cskaoyan.market.vo.SpecificationVo;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@RestController
@RequestMapping("wx/goods")
public class WxGoodsController {
    @Autowired
    WxGoodsService wxGoodsService;
    @Autowired
    WxCollectService wxCollectService;
    @Autowired
    WxIssueService wxIssueService;
    @Autowired
    WxCommentService wxCommentService;
    @Autowired
    WxGoodsSpecificationService wxGoodsSpecificationService;
    @Autowired
    WxGoodsProductService wxGoodsProductService;
    @Autowired
    WxGoodsAttributeService wxGoodsAttributeService;
    @Autowired
    WxBrandService wxBrandService;
    @Autowired
    SystemConfig systemConfig;

    @GetMapping("detail")
    @FootprintAdd
    public Object detail(Integer id) throws ExecutionException, InterruptedException {
        if (id==null||id.toString().equals("NaN")) {
            return ResponseUtil.badArgument();
        }
        Map<String, Object> data = new HashMap<>();
        MarketGoods info = wxGoodsService.getGoodsById(id);
        Integer userHasCollect = wxCollectService.getCollectNumberByGoodsId(id);
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        Callable<List<MarketIssue>> issueListCallable = () -> wxIssueService.queryIssue();
        FutureTask<List<MarketIssue>> issueCallableTask = new FutureTask<>(issueListCallable);
        Callable<CommentVo> commentListCallable = () -> wxCommentService.queryCommentVo(id);
        FutureTask<CommentVo> commentsCallableTask = new FutureTask<>(commentListCallable);
        Callable<List<SpecificationVo>> specificationCallable = () -> wxGoodsSpecificationService.querySpecification(id);
        FutureTask<List<SpecificationVo>> objectCallableTask = new FutureTask<>(specificationCallable);
        Callable<List<MarketGoodsProduct>> productListCallable = () -> wxGoodsProductService.queryProducts(id);
        FutureTask<List<MarketGoodsProduct>> productListCallableTask = new FutureTask<>(productListCallable);
        Callable<List<MarketGoodsAttribute>> attributeCallable = () -> wxGoodsAttributeService.queryAttributes(id);
        FutureTask<List<MarketGoodsAttribute>> goodsAttributeListTask = new FutureTask<>(attributeCallable);
        Callable<MarketBrand> brandCallable = () -> wxBrandService.getById(info.getBrandId());
        FutureTask<MarketBrand> brandCallableTask = new FutureTask<>(brandCallable);
        executorService.submit(issueCallableTask);
        executorService.submit(commentsCallableTask);
        executorService.submit(objectCallableTask);
        executorService.submit(productListCallableTask);
        executorService.submit(goodsAttributeListTask);
        executorService.submit(brandCallableTask);
        // goods的信息
        data.put("info", info);
// 已经收藏该商品的用户数量
        data.put("userHasCollect", userHasCollect);
// 后台管理中的通用问题

        data.put("issue", issueCallableTask.get());
// 商品评论
        data.put("comment", commentsCallableTask.get());
// 规格列表 goods_specification表 → 注意按照响应结果的格式来封装
        data.put("specificationList", objectCallableTask.get());
// 货品信息 goods_product表
        data.put("productList", productListCallableTask.get());
// 参数信息 goods_attribute表
        data.put("attribute", goodsAttributeListTask.get());
// 制造商信息
        data.put("brand", brandCallableTask.get());
// 是否分享
        data.put("share", systemConfig.isAutoCreateShareImage());
// 商品分享图片地址
        data.put("shareImage", info.getShareUrl());
        return ResponseUtil.ok(data);
    }

    @GetMapping("related")
    public Object related(Integer id) {
        MarketGoods goods = wxGoodsService.getGoodsById(id);
        //查找6个相同品类的商品,相同品类商品不足6条则只显示有的
        Integer categoryId = goods.getCategoryId();
        Integer limited = 6;
        //(0,limited)表示只取查找结果的前6条，如果是(1,limited)则表示从第2条开始取6条
        List<MarketGoods> relatedGoodsList = wxGoodsService.queryByCategoryId(id,categoryId, 0, limited);
        return ResponseUtil.okList(relatedGoodsList);
    }

    @GetMapping("category")
    public Object category(Integer id) {
        Map<String, Object> map = wxGoodsService.goodsCategory(String.valueOf(id));
        return ResponseUtil.ok(map);
    }

    @GetMapping("list")
    public Object list(GoodBo goodBo) {
        Integer limitParam = goodBo.getLimit();
        Integer pageParam = goodBo.getPage();
        String categoryId = goodBo.getCategoryId();
        String keyword = goodBo.getKeyword();
//        List<MarketGoods> list=wxGoodsService.goodsList(categoryId,limitParam,pageParam);
//        List<MarketCategory> filterList=wxGoodsService.filterList(categoryId);
        List<MarketCategory> filterList = wxGoodsService.filterList1(goodBo);
        List<MarketGoods> list = wxGoodsService.goodsList1(goodBo);

        Map<String, Object> data = new HashMap<>();
        Page page1 = (Page) list;
        data.put("total", page1.getTotal());
        data.put("page", page1.getPageNum());
        data.put("limit", page1.getPageSize());
        data.put("pages", page1.getPages());
        data.put("list", list);
        data.put("filterCategoryList", filterList);
        return ResponseUtil.ok(data);

    }

    @GetMapping("count")
    public Object count() {
        Integer goodsCount = wxGoodsService.goodsCount();
        return ResponseUtil.ok(goodsCount);
    }

}
